import { http } from '@renderer/plugins/axios'
import useUtil from './useUtil'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'

export default () => {
  const projectList = ref<ApiPage<ProjectModel>>()
  const projectFileList = ref<ApiPage<ShareFileModel>>()
  const dirFileParams = reactive({
    work_id: 0,
    dir_name: '',
    page: 1,
    limit: 21,
  })
  const getWorkList = useUtil().request(async (params) => {
    try {
      projectList.value = await http.request({
        url: '/getWorkList',
        method: 'GET',
        params: params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getWorkDirDetail = useUtil().request(
    async (params: { work_id: number; dir_name: string; limit: number; page: number }) => {
      try {
        const queryParams = Object.assign({}, dirFileParams, params)
        projectFileList.value = await http.request({
          url: '/getWorkDirDetail',
          method: 'GET',
          params: queryParams,
        })
      } catch (error) {
        console.log(error)
      }
    },
  )
  const delWorkFile = useUtil().request(async (params: { work_id: number; id: number }) => {
    try {
      await http.request({
        url: '/delWorkFile',
        method: 'POST',
        data: params,
      })
      ElMessage({ type: 'success', message: '删除成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const updateWorkFile = useUtil().request(async (params: { work_id: number; id: number; file_name: string }) => {
    try {
      await http.request({
        url: '/updateWorkFile',
        method: 'POST',
        data: params,
      })
      ElMessage({ type: 'success', message: '修改成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const workUploadFile = useUtil().request(
    async (data: { work_id: number; file_name: string; file_url: string; file_size: string; dir_name: string }) => {
      try {
        await http.request({
          url: '/workUploadFile',
          method: 'POST',
          data,
        })
        ElMessage({ type: 'success', message: '上传成功' })
      } catch (error) {
        console.log(error)
      }
    },
  )
  return {
    getWorkList,
    projectList,
    getWorkDirDetail,
    dirFileParams,
    projectFileList,
    delWorkFile,
    updateWorkFile,
    workUploadFile,
  }
}
